package jpb.module.rest.department;

import jpb.constant.Reply;
import jpb.constant.ReturnState;
import jpb.module.daomain.department.Department;
import jpb.module.service.department.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by haol on 2017/4/5.
 */
@RestController
@RequestMapping("/api/web/department")
public class DepartmentController {

        @Autowired
        private DepartmentService departmentService;

        /**
         * @api {POST} /api/web/department/i/{oid} 增加部门
         * @apiGroup Department
         * @apiName add
         * @apiDescription 给id为{oid}的组织增加新的部门
         * @apiParam {String} name 部门名(not null，unique)
         * @apiParam {String} introduce 部门介绍
         * @apiParam {Integer} parentId 父部门的id,没有则不填
         * @apiParamExample {json} 请求样例：
         * POST /api/web/organize/i/organize_id
         * {
         * "name":"计算机工程学院";
         * "introduce":"部门信息",
         * "parentId":"1";
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"增加成功"
         * }
         */
        @RequestMapping(value = "/i/{oid}", method = RequestMethod.POST)
        public String add(Department department) {
                Department dep = departmentService.getByOidAndDname(
                        department.getOid(), department.getName());
                Reply reply = null;
                if (dep == null) {
                        departmentService.add(department);
                        reply = new Reply(0, ReturnState.INSERT_SUCCESS);
                } else {
                        reply = new Reply(0, ReturnState.NO_UNIQUE);
                }
                return reply.toJson();
        }


        /**
         * @api {DELETE} /api/web/department/i/{oid}/{id} 删除部门ByDid
         * @apiGroup Department
         * @apiName delete
         * @apiDescription 删除id为{oid}的组织中id为{id}部门信息
         * @apiParamExample {json} 请求样例：
         * DELETE /api/web/organize/i/organize_id/department_id
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"删除成功"
         * }
         */
        @RequestMapping(value = "/i/{oid}/{id}", method = RequestMethod.DELETE)
        public String delete(@PathVariable String oid, @PathVariable Integer id) {
                if (departmentService.checkIsExist(oid, id)) {
                        departmentService.delete(id);
                }
                return new Reply(0, ReturnState.DELETE_SUCCESS).toJson();
        }

        /**
         * @api {POST} /api/web/department/i/{oid}/{id} 更新部门信息
         * @apiGroup Department
         * @apiName update
         * @apiDescription 更新id为{oid}的组织中部门id为{id}的部门信息（更新时需判断部门名是否为unique）
         * @apiParam {String} name 部门名(not null，unique)
         * @apiParam {String} introduce 部门介绍
         * @apiParamExample {json} 请求样例：
         * POST /api/web/organize/i/organize_id/parent_id
         * {
         * "name":"计算机工程学院";
         * "introduce":"部门信息",
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"更新成功"
         * }
         */
        @RequestMapping(value = "/i/{oid}/{id}", method = RequestMethod.POST)
        public String update(Department department) {
                Department dep1 = departmentService.getByOidAndDname(
                        department.getOid(),department.getName());
                if (dep1 != null && dep1.getId()  != department.getId()){
                                return new Reply(1, ReturnState.NO_UNIQUE).toJson();
                }
                departmentService.update(department);
                return new Reply(0, ReturnState.UPDATE_SUCCESS).toJson();
        }

        /**
         * @api {GET} /api/web/department/i/{oid}/{id} 获取部门信息ByDid
         * @apiGroup Department
         * @apiName get
         * @apiDescription 获取id为{oid}的组织中部门id为{id}的部门信息
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/i/organize_id/department_id
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功","data":{"id":36,"name":"14软件1班","parentId":31,"introduce":"班主任：邵晓峰","child":[]}
         * }
         */
        @RequestMapping(value = "/i/{oid}/{id}", method = RequestMethod.GET)
        public String get(@PathVariable Integer id, @PathVariable String oid) {
                if (!departmentService.checkIsExist(oid, id)) {
                        return new Reply(1, ReturnState.FAIL).toJson();
                }
                Department department = departmentService.get(id);
                Reply<Department> reply = new Reply<Department>(0, ReturnState.SUCCESS, department);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/web/department/i/{oid}/dep-deaf 获取组织叶子
         * @apiGroup Department
         * @apiName getDepDeaf
         * @apiDescription 获取id为{oid}的组织地叶子部门信息
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/i/organize_id/dep-deaf
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功","data":[{"id":34,"name":"14会计1班","parentId":33,"introduce":"不填写","child":[]},{"id":35,"name":"14英语1班","parentId":32,"introduce":"不填写","child":[]},{"id":37,"name":"14软件2班","parentId":31,"introduce":"班主任：不知道","child":[]},{"id":38,"name":"班干群","parentId":36,"introduce":"不填写","child":[]},{"id":39,"name":"团干群","parentId":36,"introduce":"不填写","child":[]},{"id":40,"name":"班群","parentId":36,"introduce":"不填写","child":[]}]
         * }
         */
        @RequestMapping(value = "/i/{oid}/dep-deaf")
        public String getDepDeaf(@PathVariable String oid) {
                List<Department> departments = departmentService.getLeafByOid(oid);
                if (departments.size() ==0){
                        return new Reply(3,ReturnState.NO_DATA).toJson();
                }
                Reply<List<Department>> reply = new Reply<List<Department>>(0, ReturnState.SUCCESS, departments);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/web/department/i/{oid}/dep-tree 获取组织树
         * @apiGroup Department
         * @apiName getDepTree
         * @apiDescription 获取id为{oid}组织的叶子部门信息
         * @apiParamExample {json} 请求样例：
         * GET /api/web/organize/i/organize_id/dep-tree
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"成功","data":[{"id":34,"name":"14会计1班","parentId":33,"introduce":"不填写","child":[]},{"id":35,"name":"14英语1班","parentId":32,"introduce":"不填写","child":[]},{"id":37,"name":"14软件2班","parentId":31,"introduce":"班主任：不知道","child":[]},{"id":38,"name":"班干群","parentId":36,"introduce":"不填写","child":[]},{"id":39,"name":"团干群","parentId":36,"introduce":"不填写","child":[]},{"id":40,"name":"班群","parentId":36,"introduce":"不填写","child":[]}]
         * }
         *
         */
        @RequestMapping(value = "/i/{oid}/dep-tree")
        public String getDepTree(@PathVariable String oid) {
                List<Department> departments = departmentService.getTree(oid);

                if (departments.size() ==0){
                        return new Reply(3,ReturnState.NO_DATA).toJson();
                }
                Reply<List<Department>> reply = new Reply<List<Department>>(0, ReturnState.SUCCESS, departments);
                return reply.toJson();
        }


        @RequestMapping(value = "/i/{oid}/{id}/update-logo")
        public String updateLogo(Department department) {
                departmentService.updateLogo(department);
                return ReturnState.UPDATE_SUCCESS;
        }
}
